import 'package:url_launcher/url_launcher_string.dart';
import '../../../../utils/loading.dart';
import 'dart:io';
import 'package:amap_flutter_base/amap_flutter_base.dart';

class MapNavUtils {
  /// 腾讯地图
  static Future<bool> gotoTencentMap(latitude, longitude) async {
    var url =
        'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    url = Uri.encodeFull(url);
    bool canLaunch = await canLaunchUrlString(url);

    if (!canLaunch) {
      jumpAppleMap(latitude, longitude, toastStr: '请安装腾讯地图~');
      return false;
    }

    await launchUrlString(url);

    return canLaunch;
  }

  static Future<bool> gotoAMapByKeywords(keywords, latitude, longitude,
      {LatLng? locationPoint, String? saddress}) async {
    var url = Platform.isAndroid
        ? 'amapuri://route/plan/?sourceApplication=appname&dname=$keywords&dlat=$latitude&dlon=$longitude&dev=0&t=0&pkg=com.autonavi.minimap'
        : 'iosamap://navi?sourceApplication=amap&poiname=$keywords&lat=$latitude&lon=$longitude&dev=1&style=2';
    url = Uri.encodeFull(url);
    bool isInstallApp = true;
    if (Platform.isAndroid) {
      try {
        isInstallApp = await launchUrlString(url);
      } catch (e) {
        isInstallApp = false;
        toast('请安装高德地图~');
      }
    } else {
      isInstallApp = await canLaunchUrlString(url);
      if (!isInstallApp) {
        gotoAppleMap(latitude, longitude, toAddress: saddress ?? '');
        return false;
      }
      url = Uri.encodeFull(
          'iosamap://path?sourceApplication=applicationName&dlat=$latitude&dlon=$longitude&dname=$keywords&dev=0&t=0');
      await launchUrlString(url);
      return isInstallApp;
    }

    return isInstallApp;
  }

  static Future<bool> gotoBaiduByKeywords(keywords, latitude, longitude,
      {double slat = 0, double slon = 0, String saddress = ''}) async {
    var url =
        'baidumap://map/direction?destination=name:${keywords ?? ''}|latlng:$latitude,$longitude&coord_type=bd09ll&mode=driving';
    url = Uri.encodeFull(url);
    bool canLaunch = await canLaunchUrlString(url);
    if (!canLaunch) {
      jumpAppleMap(latitude, longitude,
          toAddress: saddress, toastStr: '请安装百度地图');
      return false;
    }
    await launchUrlString(url);
    return canLaunch;
  }

  /// 苹果地图
  static Future<bool> gotoAppleMap(latitude, longitude,
      {String toAddress = ''}) async {
    var url =
        'http://maps.apple.com/?daddr=$latitude,$longitude&address=$toAddress';
    url = Uri.encodeFull(url);
    print('url=$url');
    bool canLaunchUrl = await launchUrlString(url);

    if (!canLaunchUrl) {
      toast('打开失败~');
      ;
      return false;
    }

    await launchUrlString(url);
    return true;
  }

  //没有安装地图的时候ios跳转苹果地图，安装提示安装
  static void jumpAppleMap(latitude, longitude,
      {String toAddress = '', String toastStr = ''}) {
    if (Platform.isAndroid) {
      toast(toastStr);
    } else {
      gotoAppleMap(latitude, longitude, toAddress: toAddress);
    }
  }
}
